// 用户角色类型
export type UserRole = 'admin' | 'seller' | 'customer_service';

// 用户类型
export interface User {
  id: string;
  username: string;
  email: string;
  phone?: string;
  avatar?: string;
  role: UserRole;
  shopId?: string; // 商家关联的店铺ID
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

// 店铺类型
export interface Shop {
  _id: string;
  name: string;
  description: string;
  logo: string;
  banner: string;
  owner: {
    _id: string;
    name: string;
    email: string;
    avatar?: string;
  };
  status: 'active' | 'inactive' | 'suspended' | 'pending';
  type: 'official' | 'flagship' | 'authorized' | 'personal';
  categories: string[];
  rating: number;
  reviewCount: number;
  certification: {
    isVerified: boolean;
    verifiedAt?: string;
    businessLicense?: string;
    certificationDocs?: string[];
  };
  stats: {
    totalProducts: number;
    totalSales: number;
    totalOrders: number;
    monthlyRevenue: number;
    followers: number;
  };
  contact?: {
    phone?: string;
    email?: string;
    address?: string;
    workingHours?: string;
  };
  announcement?: string;
  tags?: string[];
  createdAt: string;
  updatedAt: string;
}

// 商品类型
export interface Product {
  id: string;
  _id?: string; // 兼容后端返回的_id字段
  name: string;
  description: string;
  price: number;
  originalPrice?: number;
  images: string[];
  category: string;
  brand?: string;
  shopId: string;
  shopName?: string;
  stock: number;
  salesCount?: number; // 销售数量
  status: 'active' | 'inactive' | 'out_of_stock';
  specifications?: Record<string, string[]>;
  tags?: string[];
  isFeatured?: boolean;
  createdAt: string;
  updatedAt: string;
}

// 订单类型
export interface Order {
  _id: string;
  orderNumber: string;
  userId: string;
  userName: string;
  shopId: string;
  shopName: string;
  items: OrderItem[];
  totalAmount: number;
  shippingFee: number;
  finalAmount: number;
  status: 'pending' | 'paid' | 'shipped' | 'delivered' | 'cancelled';
  shippingAddress: ShippingAddress;
  paymentMethod: string;
  paymentResult?: PaymentResult;
  createdAt: string;
  updatedAt: string;
}

// 订单项类型
export interface OrderItem {
  productId: string;
  name: string;
  image: string;
  price: number;
  quantity: number;
  specifications?: Record<string, string>;
}

// 收货地址类型
export interface ShippingAddress {
  name: string;
  phone: string;
  province: string;
  city: string;
  district: string;
  address: string;
  postalCode?: string;
}

// 支付结果类型
export interface PaymentResult {
  id: string;
  status: string;
  update_time: string;
  email_address?: string;
}

// 优惠券类型
export interface Coupon {
  _id: string;
  name: string;
  code: string;
  type: 'fixed' | 'percentage' | 'shipping';
  value: number;
  minAmount: number;
  maxDiscount?: number;
  shopId?: string; // 店铺优惠券，为空则是平台优惠券
  shopName?: string;
  usageLimit: number;
  usedCount: number;
  isActive: boolean;
  startDate: string;
  endDate: string;
  createdAt: string;
  updatedAt: string;
}

// 轮播图类型
export interface Banner {
  _id: string;
  id: string;
  title: string;
  image: string;
  link: string;
  linkType: 'product' | 'category' | 'shop' | 'external' | 'none';
  linkTarget: string;
  order: number;
  isActive: boolean;
  isValid?: boolean;
  startDate: string;
  endDate?: string;
  clickCount: number;
  description: string;
  platform: 'mobile' | 'web' | 'all';
  createdBy?: {
    _id: string;
    name: string;
    email: string;
  };
  updatedBy?: {
    _id: string;
    name: string;
    email: string;
  };
  createdAt: string;
  updatedAt: string;
}

// 客服消息类型
export interface ChatMessage {
  _id: string;
  sessionId: string;
  senderId: string;
  senderName: string;
  senderType: 'customer' | 'service';
  content: string;
  messageType: 'text' | 'image' | 'file';
  isRead: boolean;
  createdAt: string;
}

// 客服会话类型
export interface ChatSession {
  _id: string;
  customerId: string;
  customerName: string;
  shopId: string;
  shopName: string;
  serviceId?: string;
  serviceName?: string;
  status: 'waiting' | 'active' | 'closed';
  lastMessage?: string;
  lastMessageTime?: string;
  unreadCount: number;
  createdAt: string;
  updatedAt: string;
}

// 销售统计类型
export interface SalesStats {
  totalSales: number;
  totalOrders: number;
  totalProducts: number;
  totalCustomers: number;
  dailySales: Array<{
    date: string;
    sales: number;
    orders: number;
  }>;
  topProducts: Array<{
    productId: string;
    name: string;
    sales: number;
    revenue: number;
  }>;
  topShops: Array<{
    shopId: string;
    name: string;
    sales: number;
    revenue: number;
  }>;
}

// API响应类型
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
}

// 分页参数类型
export interface PaginationParams {
  page: number;
  limit: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

// 分页响应类型
export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

// 权限类型
export interface Permission {
  id: string;
  name: string;
  code: string;
  description: string;
  module: string;
}

// 角色权限类型
export interface RolePermission {
  role: UserRole;
  permissions: string[];
}

// 登录请求类型
export interface LoginRequest {
  username: string;
  password: string;
}

// 登录响应类型
export interface LoginResponse {
  user: User;
  token: string;
  permissions: string[];
}

// 表单状态类型
export interface FormState {
  loading: boolean;
  error?: string;
}

// 表格列配置类型
export interface TableColumn {
  title: string;
  dataIndex: string;
  key: string;
  width?: number;
  fixed?: 'left' | 'right';
  sorter?: boolean;
  render?: (value: any, record: any, index: number) => React.ReactNode;
}

// 菜单项类型
export interface MenuItem {
  key: string;
  label: string;
  icon?: React.ReactNode;
  children?: MenuItem[];
  path?: string;
  permission?: string;
}

// WebSocket消息类型
export interface WebSocketMessage {
  type: 'chat' | 'notification' | 'system';
  data: any;
  timestamp: string;
}
